home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Program: Kibitz
- ** File: kibitzwindow.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1991 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "Kibitz.h" /* Get the Kibitz includes/typedefs, etc. */
- #include "KibitzCommon.h" /* Get the stuff in common with rez. */
- #include "Kibitz.protos" /* Get the prototypes for Kibitz. */
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TEXTEDITCONTROL__
- #include <TextEditControl.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UTILITIES__
- #include <Utilities.h>
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern short gPrintPage; /* Non-zero means we are printing. */
-
- static void ImageBoardLines(short hOffset, short vOffset);
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- Boolean Algebraic(FileRecHndl frHndl, short printMoveNum, short gameIndex, StringPtr pstr)
- {
- short from, to, piece, extend, rowMatch, colMatch;
- short i, r, c, rr, cc, rrr, ccc, fff, ttt, ppp;
- short color, kingLoc;
- GameListHndl gameMoves;
- MoveListHndl legalMoves;
-
- gameMoves = (*frHndl)->doc.gameMoves;
- from = (**gameMoves)[printMoveNum].moveFrom;
- to = (**gameMoves)[printMoveNum].moveTo;
-
- if (!from) {
- GetIndString(pstr, rGameStat, to);
- while (pstr[1] == ' ') BlockMove(pstr + 2, pstr + 1, --pstr[0]);
- return(false);
- }
-
- r = from / 10;
- c = from - 10 * r - 1;
- r -= 2;
-
- rr = to / 10;
- cc = to - 10 * rr - 1;
- rr -= 2;
-
- piece = (*frHndl)->doc.theBoard[from];
- if (piece < 0) piece = -piece;
-
- pstr[0] = 0;
- if (piece == PAWN) {
- if (c != cc) {
- pstr[++pstr[0]] = "abcdefgh"[c];
- pstr[++pstr[0]] = 'x';
- }
- }
- if (piece == KING) {
- i = c - cc;
- if (i == 2) {
- pstrcpy(pstr, "\pO-O-O");
- piece = EMPTY;
- }
- if (i == -2) {
- pstrcpy(pstr, "\pO-O");
- piece = EMPTY;
- }
- }
-
- if (piece > PAWN) {
- rowMatch = colMatch = extend = false;
- GenerateLegalMoves(frHndl);
- legalMoves = (*frHndl)->doc.legalMoves;
- for (i = 0; i < (*frHndl)->doc.numLegalMoves; ++i) {
- fff = (**legalMoves)[i].moveFrom;
- ttt = (**legalMoves)[i].moveTo;
- if ((ttt == to) && (fff != from)) {
- ppp = (*frHndl)->doc.theBoard[fff];
- if (ppp < 0) ppp = -ppp;
- if (ppp != piece) continue;
- rrr = fff / 10;
- ccc = fff - 10 * rrr - 1;
- rrr -= 2;
- extend = true;
- if (r == rrr) rowMatch = true;
- if (c == ccc) colMatch = true;
- }
- }
- if ((extend) && (!colMatch)) rowMatch |= extend;
- pstr[++pstr[0]] = " NBRQK"[piece];
- if (rowMatch)
- pstr[++pstr[0]] = "abcdefgh"[c];
- if (colMatch)
- pstr[++pstr[0]] = "87654321"[r];
- i = (*frHndl)->doc.theBoard[to];
- if ((i) && (i != OBNDS)) pstr[++pstr[0]] = 'x';
-
- }
- if (piece) {
- pstr[++pstr[0]] = "abcdefgh"[cc];
- pstr[++pstr[0]] = "87654321"[rr];
- }
- if (piece == PAWN) {
- if (to = (**gameMoves)[printMoveNum].promoteTo) {
- if (to < 0) to = -to;
- pstr[++pstr[0]] = '=';
- pstr[++pstr[0]] = " NBRQ"[to];
- }
- }
-
- if (printMoveNum < (*frHndl)->doc.numGameMoves) {
- RepositionBoard(frHndl, printMoveNum + 1, false);
- kingLoc = (*frHndl)->doc.king[color = WhosMove(frHndl)].kingLoc;
- if (SquareAttacked(frHndl, kingLoc, color)) pstr[++pstr[0]] = '+';
- RepositionBoard(frHndl, printMoveNum, false);
- }
-
- return(printMoveNum == gameIndex - 1);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This function adds the application's controls to a window. */
-
- #pragma segment Window
- OSErr AppNewWindowControls(FileRecHndl frHndl, WindowPtr window)
- {
- OSErr err;
- TEHandle mssgIn, mssgOut;
- ControlHandle sendMssg, beepOnMove, beepOnMssg;
- Rect destRect, viewRect, brdrRect;
- ControlHandle gameSlider, whiteStarts, blackStarts, resign, draw;
- ControlHandle record, sendSnd;
- Handle textHndl;
-
- SetRect(&brdrRect,
- kBoardWidth + 22,
- 35,
- rWindowWidth - 20,
- kBoardHalfHeight + kBoardVOffset - 1
- );
- destRect = brdrRect;
- InsetRect(&destRect, 4, 4);
- viewRect = destRect;
- destRect.right -= 2;
- /* This fixes a TextEdit problem where the view has to be a little
- ** outside the dest on the right, or else characters are clipped.
- */
-
- CTENew(rViewCtl, /* viewCtl of resID for TextEdit control. */
- window, /* Window to hold TERecord. */
- &mssgIn, /* Return handle for TERecord. */
- &destRect, /* destRect for TERecord */
- &viewRect, /* viewRect for TERecord */
- &brdrRect, /* Used to frame a border. */
- 32000, /* Maximum TextEdit document length. */
- cteReadOnly+cteVScroll /* TERecord is regular read-only. */
- );
-
- OffsetRect(&destRect, 0, kBoardHalfHeight - 38);
- OffsetRect(&viewRect, 0, kBoardHalfHeight - 38);
- OffsetRect(&brdrRect, 0, kBoardHalfHeight - 38);
- CTENew(rViewCtl, window, &mssgOut, &destRect, &viewRect, &brdrRect, 32000, cteVScroll);
-
- if (mssgOut) {
- textHndl = (Handle)(*frHndl)->doc.legalMoves;
- (*frHndl)->doc.legalMoves = (MoveListHndl)CTESwapText(mssgOut, textHndl, false);
- /* AppOpenDocument may have placed some text for the out-box TextEdit
- ** control temporarily in the legalMoves handle. Move this text into
- ** the out-box TextEdit control. */
- }
-
- if (sendMssg = GetNewControl(rSendMessage, window)) HiliteControl(sendMssg, 255);
- if (beepOnMove = GetNewControl(rMoveNotify, window)) HiliteControl(beepOnMove, 255);
- if (beepOnMssg = GetNewControl(rMssgNotify, window)) HiliteControl(beepOnMssg, 255);
-
- if (whiteStarts = GetNewControl(rWhiteStarts, window)) {
- OffsetControl(whiteStarts, -4096, 0);
- SetCtlValue(whiteStarts, (*frHndl)->doc.startColor ^ 1);
- ShowControl(whiteStarts);
- }
- if (blackStarts = GetNewControl(rBlackStarts, window)) {
- OffsetControl(blackStarts, -4096, 0);
- SetCtlValue(blackStarts, (*frHndl)->doc.startColor);
- ShowControl(blackStarts);
- }
-
- resign = GetNewControl(rResign, window);
- draw = GetNewControl(rDraw, window);
- if (record = GetNewControl(rRecordSound, window)) HiliteControl(record, 255);
- if (sendSnd = GetNewControl(rSendSound, window)) HiliteControl(sendSnd, 255);
-
- gameSlider = BoardSliderNew(window);
-
- (*frHndl)->doc.message[kMessageIn] = mssgIn;
- (*frHndl)->doc.message[kMessageOut] = mssgOut;
- (*frHndl)->doc.sendMessage = sendMssg;
- (*frHndl)->doc.beepOnMove = beepOnMove;
- (*frHndl)->doc.beepOnMssg = beepOnMssg;
- (*frHndl)->doc.gameSlider = gameSlider;
- (*frHndl)->doc.wbStart[0] = whiteStarts;
- (*frHndl)->doc.wbStart[1] = blackStarts;
- (*frHndl)->doc.resign = resign;
- (*frHndl)->doc.draw = draw;
- (*frHndl)->doc.record = record;
- (*frHndl)->doc.sendSnd = sendSnd;
-
- if (
- (mssgIn) &&
- (mssgOut) &&
- (sendMssg) &&
- (beepOnMove) &&
- (beepOnMssg) &&
- (resign) &&
- (draw) &&
- (record) &&
- (sendSnd)
- ) {
- AdjustGameSlider(frHndl);
- err = noErr;
- }
- else
- err = memFullErr;
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void DrawTime(FileRecHndl frHndl)
- {
- WindowPtr oldPort;
- Rect clockRect;
- short clock, i, time[3];
- unsigned long timeLeft, displayTime;
- char pstr[5], timestr[10];
-
- if ((*frHndl)->doc.arrangeBoard) return;
-
- oldPort = SetFilePort(frHndl);
-
- TextMode(srcCopy);
- TextFont(systemFont);
-
- for (clock = 0; clock < 2; ++clock) {
-
- clockRect = BoardRect();
-
- if (clock == (*frHndl)->doc.invertBoard)
- clockRect.top += 14;
-
- clockRect.left = clockRect.right + 4;
- clockRect.right = clockRect.left + 70;
- clockRect.bottom = clockRect.top + 14;
-
- timeLeft = (*frHndl)->doc.timeLeft[clock];
- if (timeLeft == -1) {
- EraseRect(&clockRect);
- continue;
- }
-
- if ((displayTime = (*frHndl)->doc.displayTime[clock]) > timeLeft)
- displayTime = (*frHndl)->doc.displayTime[clock] = timeLeft;
-
- MoveTo(clockRect.left + 6, clockRect.top + 14);
-
- for (i = 3; i;) {
- displayTime /= 60;
- time[--i] = displayTime % 60;
- }
- timestr[0] = 0;
- for (i = 0; i < 3; ++i) {
- i2pstr(pstr, time[i]);
- if (pstr[0] == 1) pstrcat(timestr, "\p0");
- pstrcat(timestr, pstr);
- pstrcat(timestr, "\1:\1:\1 " + (i << 1)); /* Append colon or space. */
- }
- DrawString(timestr);
- }
-
- TextMode(srcOr);
- SetPort(oldPort);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void ImageBoardLines(short hOffset, short vOffset)
- {
- short i;
-
- PenNormal();
- PenSize(2, 2);
-
- for (i = 0; i <= 8; ++i) {
- MoveTo(hOffset, vOffset + kBoardSqSize * i);
- Line(kBoardSqSize * 8, 0);
- MoveTo(hOffset + kBoardSqSize * i, vOffset);
- Line(0, kBoardSqSize * 8);
- }
-
- PenNormal();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* Image the document into the current port. */
-
- #pragma segment Window
- void ImageDocument(FileRecHndl frHndl, Boolean justBoard)
- {
- #pragma unused (frHndl)
-
- short r, c, rr, cc, piece, pieceIconID, fnum;
- short gameIndex, lastFrom, lastTo, square, hOffset, vOffset;
- Boolean invertBoard;
- Handle pieceIcon;
- Rect sqRect, theInk;
- GameListHndl gameMoves;
- WindowPtr thePort;
-
- hOffset = kBoardHOffset;
- vOffset = kBoardVOffset;
- if (gPrintPage) {
- GetPort(&thePort);
- theInk = thePort->portRect;
- GetFNum("\pTimes", &fnum);
- TextFont(fnum);
- TextSize(12);
- TextFace(normal);
- hOffset = theInk.right - 10 - kBoardWidth;
- vOffset = 10;
- }
- else ImageBoardLines(hOffset, vOffset);
-
- invertBoard = (*frHndl)->doc.invertBoard;
- lastFrom = lastTo = 0;
- gameIndex = (*frHndl)->doc.gameIndex;
- gameMoves = (*frHndl)->doc.gameMoves;
- if (gameIndex) {
- lastFrom = (**gameMoves)[--gameIndex].moveFrom;
- lastTo = (**gameMoves)[gameIndex++].moveTo;
- }
-
- if (gPrintPage < 2) { /* If not printing, or printing first page... */
- for (r = 0; r < 8; ++r) {
- for (c = 0; c < 8; ++c) {
- piece = (*frHndl)->doc.theBoard[square = START_IBNDS + 10 * r + c];
- pieceIconID = piece + 257 + KING;
-
- rr = r;
- cc = c;
- if (invertBoard) {
- rr = 7 - r;
- cc = 7 - c;
- }
-
- if ((rr + cc) & 0x01) pieceIconID += 13;
- pieceIcon = GetResource('ICN#', pieceIconID);
-
- sqRect.top = vOffset + kBoardSqSize * rr + 3;
- sqRect.left = hOffset + kBoardSqSize * cc + 3;
- sqRect.bottom = sqRect.top + 32;
- sqRect.right = sqRect.left + 32;
- PlotIcon(&sqRect, pieceIcon);
-
- if ((square == lastFrom) || (square == lastTo))
- FrameRect(&sqRect);
- }
- }
- }
-
- if (gPrintPage) { /* If printing... */
- if (gPrintPage == 1) /* If printing page 1... */
- ImageBoardLines(hOffset, vOffset);
- ImageMoveList(frHndl, theInk, hOffset);
- return;
- }
-
- if (!justBoard) {
- GetPort(&thePort);
- SetOrigin((*frHndl)->doc.arrangeBoard * 4096, 0);
- UpdateGameStatus(frHndl);
- DoDrawControls(thePort, false);
- OutlineControl((*frHndl)->doc.sendMessage);
- DrawTime(frHndl);
- DrawPalette(frHndl);
- SetOrigin(0, 0);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Window
- void ImageMoveList(FileRecHndl frHndl, Rect theInk, short hOffset)
- {
- short gameIndex, numGameMoves, printMoveNum, colsPerPage, colHeight;
- short pageNum, colNum, colVOffset, numMovePairs, colEndMove, hloc, vloc;
- Str255 pstr;
-
- gameIndex = (*frHndl)->doc.gameIndex;
- numGameMoves = (*frHndl)->doc.numGameMoves;
- printMoveNum = (*frHndl)->doc.startColor;
- colsPerPage = (theInk.right - theInk.left) / 180;
- colHeight = (theInk.bottom - theInk.top) - 2 * kBoardHOffset;
-
- for (pageNum = 1; pageNum <= gPrintPage; ++pageNum) {
-
- for (colNum = 1; colNum <= colsPerPage; ++colNum) {
-
- hloc = colNum * 180 - 120;
-
- colVOffset = 0;
- if (pageNum == 1) {
- if (colNum == 1) colVOffset = (3 * 20);
- if (hloc + 130 >= hOffset)
- for (; colVOffset < (kBoardHeight + 20 + kBoardVOffset); colVOffset += 20);
- /* Start this column below the board on 20-pixel boundary. */
- }
-
- numMovePairs = (colHeight - colVOffset) / 20;
- colEndMove = printMoveNum + 2 * numMovePairs;
-
- if (pageNum == gPrintPage) {
-
- if ((pageNum == 1) && (colNum == 1)) {
- MoveTo(hloc, theInk.top + 20);
- pstrcpy(pstr, (*frHndl)->fileState.fss.name);
- TextFace(bold + underline);
- DrawString(pstr);
- TextFace(normal);
- }
-
- for (; printMoveNum < colEndMove; ++printMoveNum) {
-
- RepositionBoard(frHndl, printMoveNum, false);
-
- if (printMoveNum >= numGameMoves) {
- gPrintPage = 0;
- RepositionBoard(frHndl, gameIndex, false);
- return;
- }
-
- vloc = theInk.top + colVOffset + 20;
-
- if (!(printMoveNum & 0x01)) {
- i2pstr(pstr, printMoveNum / 2 + 1);
- MoveTo(hloc - 16 - StringWidth(pstr), vloc);
- DrawString(pstr);
- DrawString("\p)");
- MoveTo(hloc, vloc);
- }
- else {
- MoveTo(hloc + 40, vloc);
- MoveTo(hloc + 40 + 12, vloc);
- colVOffset += 20;
- }
-
- if (Algebraic(frHndl, printMoveNum, gameIndex, pstr))
- TextFace(underline);
-
- DrawString(pstr);
- TextFace(normal);
- }
- }
-
- if ((printMoveNum = colEndMove) >= numGameMoves) {
- gPrintPage = 0;
- RepositionBoard(frHndl, gameIndex, false);
- return;
- }
- }
- }
-
- RepositionBoard(frHndl, gameIndex, false);
- }
-
-
-
-